package helm

import (
	"context"
	"fmt"
	helmClient "github.com/mittwald/go-helm-client"
	"helm.sh/helm/v3/pkg/repo"
)

type HelmDriver struct {
	ctx        context.Context
	helmClient helmClient.Client
}

func OpenRepo(url, repoName, cacheDir string) (helmClient.Client, error) {
	opt := &helmClient.Options{
		Namespace:        "default", // Change this to the namespace you wish the client to operate in.
		RepositoryCache:  cacheDir + ".helmcache",
		RepositoryConfig: cacheDir + ".helmrepo",
		Debug:            true,
		Linting:          true,
		DebugLog:         func(format string, v ...interface{}) {},
	}

	helmClient, err := helmClient.New(opt)
	if err != nil {
		return nil, err
	}

	err = helmClient.AddOrUpdateChartRepo(repo.Entry{
		//Name: "gluerepo",
		Name: repoName,
		//URL:  "http://152.136.134.100:30028",
		URL: url,
	})
	if err != nil {
		return nil, err
	}

	if err = helmClient.UpdateChartRepos(); err != nil {
		return nil, fmt.Errorf("init update repo %v", err)
	}

	return helmClient, nil
}
